package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;

/**
 * 评论对话框
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/13 0013 13:58
 */
public class CommentDialog extends Dialog {

    private ImageView mIvClose;

    private TextView mTvSubmit;

    private EditText mEtComment;

    private OnSubmitListener mListener;

    public CommentDialog(@NonNull Context context) {
        super(context, R.style.BottomDialogTheme);

        setContentView(R.layout.dialog_comment);

        initWindow();
        initViews();
        initListeners();
    }

    private void initListeners() {
        mTvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onSubmit(mEtComment.getText().toString().trim());
                }
            }
        });
        mIvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initWindow() {
        // 改变样式-->window的样式
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        // 设置弹出位置
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;

        window.setAttributes(params);
    }

    private void initViews() {
        mIvClose = (ImageView) findViewById(R.id.iv_close);
        mTvSubmit = (TextView) findViewById(R.id.tv_submit);
        mEtComment = (EditText) findViewById(R.id.et_comment);
    }

    public void setOnSubmitListener(OnSubmitListener listener) {
        mListener = listener;
    }

    public interface OnSubmitListener {
        void onSubmit(String text);
    }
}
